package com.wjb.web.front;

import com.wjb.model.EbUser;
import com.wjb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired

    private UserService userService;

    //登录
    @RequestMapping("/to_login")
    public String toLogin(){

        return "login";//login.jsp
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request,String userName, String passWord){
        System.out.println("userName="+userName+"\tpassWord="+passWord);
        //1.调用service的UserService的登录的方法(根据用户和密码查询是否存在这个用户)
        EbUser lgnUser = userService.login(userName,passWord);
        System.out.println("lgnUser="+lgnUser);

        HttpSession session = request.getSession();
        if(lgnUser==null){
            //保存一个登录失败的标记
            session.setAttribute("lgn_error","lgn_error");
            //重新登录
            return "redirect:to_login";//重定向
        }

        //2.要将用户放入session中  (容器，域===>蛇皮袋)
        //保存登录的用户到session
        session.setAttribute("lgnUser",lgnUser);
        //清除错误提示
        session.removeAttribute("lgn_error");
        //3.登录成功往首页跳转
        return "redirect:index/front";
    }

    //登出

    //注册
}
